#include "bindings/bindings_artboard.h"
#include "helpers/general.h"
#include <cstdio>
#include <string>

#include "models/renderer.h"
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/animation/state_machine_instance.hpp"
#include "rive/artboard.hpp"
#include "rive/text/text_value_run.hpp"
#include "rive/viewmodel/runtime/viewmodel_instance_runtime.hpp"

namespace ohos_rive {

/**
 * 获取 Artboard 名称
 * 对应原 JNI 函数：Java_app_rive_runtime_kotlin_core_Artboard_cppName
 */
napi_value ArtboardCppName(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("failed to get callback info");
        return nullptr;
    }

    // 获取 artboard 指针
    int64_t ref;
    status = napi_get_value_int64(env, args[0], &ref);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(ref);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取名称并返回字符串
    const std::string &name = artboard->name();
    LOGI("Get artboard name: %{public}s", name.c_str());

    napi_value result;
    status = napi_create_string_utf8(env, name.c_str(), name.length(), &result);
    if (status != napi_ok) {
        LOGE("Failed to create string");
        return nullptr;
    }

    return result;
}

/**
 * 获取 Animation 名称
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppAnimationNameByIndex
 */
napi_value ArtboardAnimationNameByIndex(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("failed to get callback info");
        return nullptr;
    }

    int64_t artboardRef = 0;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }

    int32_t index = 0;
    status = napi_get_value_int32(env, args[1], &index);
    if (status != napi_ok) {
        LOGE("Failed to get animation index");
        return nullptr;
    }

    // 转换指针并获取动画名称
    auto *artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (artboard == nullptr) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    auto *animation = artboard->animation(static_cast<size_t>(index));
    if (animation == nullptr) {
        LOGE("Animation not found");
        return nullptr;
    }

    auto name = animation->name();
    LOGI("Get animation name: %{public}s", name.c_str());

    // 创建返回的字符串
    napi_value result;
    status = napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &result);
    if (status != napi_ok) {
        LOGE("Failed to create string");
        return nullptr;
    }

    return result;
}

/**
 * 获取 StateMachine 名称
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppStateMachineNameByIndex
 */
napi_value ArtboardStateMachineNameByIndex(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("failed to get callback info");
        return nullptr;
    }

    int64_t artboardRef = 0;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }

    int32_t index = 0;
    status = napi_get_value_int32(env, args[1], &index);
    if (status != napi_ok) {
        LOGE("Failed to get state machine index");
        return nullptr;
    }

    // 转换指针并获取状态机名称
    auto *artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (artboard == nullptr) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    auto *stateMachine = artboard->stateMachine(static_cast<size_t>(index));
    if (stateMachine == nullptr) {
        LOGE("State machine not found");
        return nullptr;
    }

    auto name = stateMachine->name();
    LOGI("Get state machine name: %{public}s", name.c_str());
    // 创建返回的字符串
    napi_value result;
    status = napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &result);
    if (status != napi_ok) {
        LOGE("Failed to create string");
        return nullptr;
    }

    return result;
}

/**
 * 获取 Animation 实例
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppAnimationByIndex
 */
napi_value ArtboardAnimationByIndex(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("failed to get callback info");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }

    int32_t index;
    status = napi_get_value_int32(env, args[1], &index);
    if (status != napi_ok) {
        LOGE("Failed to get animation index");
        return nullptr;
    }

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取动画并返回指针
    auto animation = artboard->animationAt(index);
    if (!animation) {
        LOGE("Animation not found at specified index");
        return nullptr;
    }
    // 将指针转换为int64返回
    napi_value result;
    status = napi_create_int64(env, reinterpret_cast<int64_t>(animation.release()), &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }

    return result;
}

napi_value PointerToNapiValue(napi_env env, void *ptr)
{
    napi_value result;
    napi_create_int64(env, reinterpret_cast<int64_t>(ptr), &result);
    return result;
}

/**
 * 获取 Animation 实例
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppAnimationByName
 */
napi_value ArtboardAnimationByName(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("failed to get callback info");
        return nullptr;
    }

    int64_t ref;
    status = napi_get_value_int64(env, args[0], &ref);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(ref);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 解析第二个参数（动画名称）
    std::string animationName = NapiStringToString(env, args[1]);

    // 调用原生方法
    auto animation = artboard->animationNamed(animationName);
    if (!animation) {
        LOGE("Animation not found with name: %{public}s", animationName.c_str());
        return nullptr;
    }

    // 返回动画对象的指针（作为int64_t）
    return PointerToNapiValue(env, animation.release());
}

/**
 * 获取 Animation 数量
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppAnimationCount
 */
napi_value ArtboardAnimationCount(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("failed to get callback info");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取动画数量并返回
    int count = artboard->animationCount();

    napi_value result;
    status = napi_create_int32(env, count, &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("Animation count: %{public}d", count);
    return result;
}

/**
 * 获取 StateMachine 实例
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppStateMachineByIndex
 */
napi_value ArtboardStateMachineByIndex(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and index");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }

    int32_t index;
    status = napi_get_value_int32(env, args[1], &index);
    if (status != napi_ok) {
        LOGE("Failed to get state machine index");
        return nullptr;
    }

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取状态机并返回指针
    auto stateMachine = artboard->stateMachineAt(index);
    if (!stateMachine) {
        LOGE("State machine not found at index: %{public}d", index);
        return nullptr;
    }
    // 将指针转换为int64返回
    napi_value result;
    status = napi_create_int64(env, reinterpret_cast<int64_t>(stateMachine.release()), &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    return result;
}

/**
 * 获取 StateMachine 实例
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppStateMachineByName
 */
napi_value ArtboardStateMachineByName(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and name");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取名称字符串
    std::string stateMachineName = NapiStringToString(env, args[1]);

    // 通过名称获取状态机并返回指针
    auto stateMachine = artboard->stateMachineNamed(stateMachineName);
    if (!stateMachine) {
        LOGE("State machine not found with name: %{public}s", stateMachineName.c_str());
        return nullptr;
    }

    // 将指针转换为int64返回
    napi_value result;
    status = napi_create_int64(env, reinterpret_cast<int64_t>(stateMachine.release()), &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    return result;
}

/**
 * 获取状态机数量
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppStateMachineCount
 */
napi_value ArtboardStateMachineCount(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取状态机数量并返回
    int count = artboard->stateMachineCount();

    napi_value result;
    status = napi_create_int32(env, count, &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("State machine count: %{public}d", count);
    return result;
}

/**
 * 输入名字和路径
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppInputByNameAtPath
 */
napi_value ArtboardInputByNameAtPath(napi_env env, napi_callback_info info)
{
    size_t argc = 3;
    napi_value args[3];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 3) {
        LOGE("Invalid arguments - expected artboard reference, name and path");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    // 获取名称字符串
    std::string inputName = NapiStringToString(env, args[1]);
    // 获取路径字符串
    std::string inputPath = NapiStringToString(env, args[2]);

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 通过名称和路径获取输入并返回指针
    auto input = artboard->input(inputName, inputPath);
    if (!input) {
        LOGE("artboard input failure: %{public}s, path: %{public}s", inputName.c_str(), inputPath.c_str());
        return nullptr;
    }

    // 将指针转换为int64返回
    napi_value result;
    status = napi_create_int64(env, reinterpret_cast<int64_t>(input), &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("Input sucess: %{public}s, path: %{public}s", inputName.c_str(), inputPath.c_str());
    return result;
}

/**
 * 获取音量
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppGetVolume
 */
napi_value ArtboardGetVolume(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取音量并返回
    float volume = artboard->volume();

    napi_value result;
    status = napi_create_double(env, volume, &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("Get volume: %{public}f", volume);
    return result;
}

/**
 * 设置音量
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppSetVolume
 */
napi_value ArtboardSetVolume(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and volume");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    // 获取音量值
    double volume;
    status = napi_get_value_double(env, args[1], &volume);
    if (status != napi_ok) {
        LOGE("Failed to get volume value");
        return nullptr;
    }

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 设置音量
    artboard->volume(static_cast<float>(volume));
    LOGI("Set volume: %{public}f", volume);

    // 返回undefined（void方法）
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

/**
 * 获取文本值运行对象
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppFindTextValueRun
 */
napi_value ArtboardFindTextValueRun(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and name");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取名称字符串
    std::string textValueRunName = NapiStringToString(env, args[1]);

    // 查找文本值运行对象
    auto textValueRun = artboard->find<rive::TextValueRun>(textValueRunName);
    if (!textValueRun) {
        LOGE("TextValueRun not found with name: %{public}s", textValueRunName.c_str());
        return nullptr;
    }

    // 将指针转换为int64返回
    napi_value result;
    status = napi_create_int64(env, reinterpret_cast<int64_t>(textValueRun), &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }

    LOGI("TextValueRun found with name: %{public}s", textValueRunName.c_str());
    return result;
}

/**
 * 获取文本值运行对象中的值
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppFindValueOfTextValueRun
 */
napi_value ArtboardFindValueOfTextValueRun(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and name");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 获取名称字符串
    std::string textValueRunName = NapiStringToString(env, args[1]);

    // 查找文本值运行对象
    auto run = artboard->find<rive::TextValueRun>(textValueRunName);
    if (run == nullptr) {
        LOGE("TextValueRun not found with name: %{public}s", textValueRunName.c_str());
        return nullptr;
    }

    // 获取文本值并返回字符串
    std::string textValue = run->text();
    napi_value result;
    status = napi_create_string_utf8(env, textValue.c_str(), textValue.length(), &result);
    if (status != napi_ok) {
        LOGE("Failed to create string result");
        return nullptr;
    }
    LOGI("TextValueRun value found - name: %{public}s, value: %{public}s", textValueRunName.c_str(), textValue.c_str());
    return result;
}

/**
 * 设置文本值运行对象中的值
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppSetValueOfTextValueRun
 */
napi_value ArtboardSetValueOfTextValueRun(napi_env env, napi_callback_info info)
{
    size_t argc = 3;
    napi_value args[3];
    napi_value falseValue;
    napi_get_boolean(env, false, &falseValue);

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 3) {
        LOGE("Invalid arguments - expected artboard reference, name and new text");
        return falseValue;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return falseValue;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return falseValue;
    }
    // 获取名称字符串
    std::string textValueRunName = NapiStringToString(env, args[1]);
    // 获取新文本字符串
    std::string newText = NapiStringToString(env, args[2]);
    // 查找文本值运行对象
    auto run = artboard->find<rive::TextValueRun>(textValueRunName);
    if (run == nullptr) {
        LOGE("TextValueRun not found with name: %{public}s", textValueRunName.c_str());
        return falseValue;
    }

    // 设置新文本值
    run->text(newText);
    LOGI("TextValueRun value set - name: %{public}s, new value: %{public}s", textValueRunName.c_str(), newText.c_str());
    // 返回true表示设置成功
    napi_value trueValue;
    napi_get_boolean(env, true, &trueValue);
    return trueValue;
}

/**
 * 通过名称和路径查找文本值运行对象
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppFindTextValueRunAtPath
 */
napi_value ArtboardFindTextValueRunAtPath(napi_env env, napi_callback_info info)
{
    size_t argc = 3;
    napi_value args[3];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 3) {
        LOGE("Invalid arguments - expected artboard reference, name and path");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 获取名称字符串
    std::string textValueRunName = NapiStringToString(env, args[1]);
    // 获取路径字符串
    std::string textValueRunPath = NapiStringToString(env, args[2]);

    // 通过名称和路径获取文本值运行对象
    auto textValueRun = artboard->getTextRun(textValueRunName, textValueRunPath);
    if (!textValueRun) {
        LOGE("TextValueRun not found with name: %{public}s, path: %{public}s", textValueRunName.c_str(),
             textValueRunPath.c_str());
        return nullptr;
    }
    // 将指针转换为int64返回
    napi_value result;
    status = napi_create_int64(env, reinterpret_cast<int64_t>(textValueRun), &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("TextValueRun found with name: %{public}s, path: %{public}s", textValueRunName.c_str(),
         textValueRunPath.c_str());
    return result;
}

/**
 * 通过名称和路径查找文本值运行对象中的值
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppFindValueOfTextValueRunAtPath
 */
napi_value ArtboardFindValueOfTextValueRunAtPath(napi_env env, napi_callback_info info)
{
    size_t argc = 3;
    napi_value args[3];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 3) {
        LOGE("Invalid arguments - expected artboard reference, name and path");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    // 获取名称字符串
    std::string textValueRunName = NapiStringToString(env, args[1]);
    // 获取路径字符串
    std::string textValueRunPath = NapiStringToString(env, args[2]);

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 通过名称和路径获取文本值运行对象
    auto run = artboard->getTextRun(textValueRunName, textValueRunPath);
    if (run == nullptr) {
        LOGE("TextValueRun not found with name: %{public}s, path: %{public}s", textValueRunName.c_str(),
             textValueRunPath.c_str());
        return nullptr;
    }

    // 获取文本值并返回字符串
    std::string textValue = run->text();
    napi_value result;
    status = napi_create_string_utf8(env, textValue.c_str(), textValue.length(), &result);
    if (status != napi_ok) {
        LOGE("Failed to create string result");
        return nullptr;
    }
    LOGI("TextValueRun value found - name: %{public}s, path: %{public}s, value: %{public}s", textValueRunName.c_str(),
         textValueRunPath.c_str(), textValue.c_str());
    return result;
}

/**
 * 设置文本值运行对象中的值
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppSetValueOfTextValueRunAtPath
 */
napi_value ArtboardSetValueOfTextValueRunAtPath(napi_env env, napi_callback_info info)
{
    size_t argc = 4;
    napi_value args[4];
    napi_value falseValue;
    napi_get_boolean(env, false, &falseValue);

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 4) {
        LOGE("Invalid arguments - expected artboard reference, name, new text and path");
        return falseValue;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return falseValue;
    }
    // 获取名称字符串
    std::string textValueRunName = NapiStringToString(env, args[1]);
    // 获取新文本字符串
    std::string newText = NapiStringToString(env, args[2]);
    // 获取路径字符串
    std::string textValueRunPath = NapiStringToString(env, args[3]);
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return falseValue;
    }

    // 通过名称和路径获取文本值运行对象
    auto run = artboard->getTextRun(textValueRunName, textValueRunPath);
    if (run == nullptr) {
        LOGE("TextValueRun not found with name: %{public}s, path: %{public}s", textValueRunName.c_str(),
             textValueRunPath.c_str());

        return falseValue;
    }
    // 设置新文本值
    run->text(newText);
    LOGI("TextValueRun value set - name: %{public}s, path: %{public}s, new value: %{public}s", textValueRunName.c_str(),
         textValueRunPath.c_str(), newText.c_str());
    napi_value trueValue;
    napi_get_boolean(env, true, &trueValue);
    return trueValue;
}

/**
 * 快进操作
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppAdvance
 */
napi_value ArtboardAdvance(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];
    napi_value falseValue;
    napi_get_boolean(env, false, &falseValue);

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and elapsed time");
        return falseValue;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return falseValue;
    }

    // 获取经过时间
    double elapsedTime;
    status = napi_get_value_double(env, args[1], &elapsedTime);
    if (status != napi_ok) {
        LOGE("Failed to get elapsed time");
        return falseValue;
    }

    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return falseValue;
    }
    // 推进动画并返回结果
    bool result = artboard->advance(static_cast<float>(elapsedTime));

    napi_value jsResult;
    status = napi_get_boolean(env, result, &jsResult);
    if (status != napi_ok) {
        LOGE("Failed to create boolean result");
        return falseValue;
    }

    LOGI("Artboard advanced by %{public}f seconds, result: %{public}s", elapsedTime, result ? "true" : "false");
    return jsResult;
}

/**
 * 获取边界创建新对象
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppBounds
 */
napi_value ArtboardBounds(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 获取边界
    const auto bounds = artboard->bounds();

    // 创建JavaScript对象
    napi_value jsBounds;
    status = napi_create_object(env, &jsBounds);
    if (status != napi_ok) {
        LOGE("Failed to create bounds object");
        return nullptr;
    }
    // 设置left属性
    napi_value leftValue;
    napi_create_double(env, bounds.left(), &leftValue);
    napi_set_named_property(env, jsBounds, "left", leftValue);

    // 设置top属性
    napi_value topValue;
    napi_create_double(env, bounds.top(), &topValue);
    napi_set_named_property(env, jsBounds, "top", topValue);

    // 设置right属性
    napi_value rightValue;
    napi_create_double(env, bounds.right(), &rightValue);
    napi_set_named_property(env, jsBounds, "right", rightValue);

    // 设置bottom属性
    napi_value bottomValue;
    napi_create_double(env, bounds.bottom(), &bottomValue);
    napi_set_named_property(env, jsBounds, "bottom", bottomValue);

    LOGI("Artboard bounds: left=%{public}f, top=%{public}f, right=%{public}f, bottom=%{public}f", bounds.left(),
         bounds.top(), bounds.right(), bounds.bottom());
    return jsBounds;
}

/**
 * 重置artboard尺寸
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppResetArtboardSize
 */
napi_value ArtboardResetArtboardSize(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 重置artboard尺寸到原始尺寸
    artboard->width(artboard->originalWidth());
    artboard->height(artboard->originalHeight());
    LOGI("Artboard size reset to original: %{public}f x %{public}f", artboard->originalWidth(),
         artboard->originalHeight());
    // 返回undefined（void方法）
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

/**
 * 获取artboard宽度
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppGetArtboardWidth
 */
napi_value ArtboardGetArtboardWidth(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 获取宽度并返回
    float width = artboard->width();

    napi_value result;
    status = napi_create_double(env, width, &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("Get artboard width: %{public}f", width);
    return result;
}

/**
 * 设置artboard宽度
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppSetArtboardWidth
 */
napi_value ArtboardSetArtboardWidth(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and width value");
        return nullptr;
    }

    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    // 获取宽度值
    double width;
    status = napi_get_value_double(env, args[1], &width);
    if (status != napi_ok) {
        LOGE("Failed to get width value");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    // 设置artboard宽度
    artboard->width(static_cast<float>(width));
    LOGI("Set artboard width: %{public}f", width);

    // 返回undefined（void方法）
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

/**
 * 获取artboard高度
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppGetArtboardHeight
 */
napi_value ArtboardGetArtboardHeight(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 获取高度并返回
    float height = artboard->height();

    napi_value result;
    status = napi_create_double(env, height, &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("Get artboard height: %{public}f", height);
    return result;
}

/**
 * 设置artboard高度
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppSetArtboardHeight
 */
napi_value ArtboardSetArtboardHeight(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and height value");
        return nullptr;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    // 获取高度值
    double height;
    status = napi_get_value_double(env, args[1], &height);
    if (status != napi_ok) {
        LOGE("Failed to get height value");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 设置artboard高度
    artboard->height(static_cast<float>(height));
    LOGI("Set artboard height: %{public}f", height);
    // 返回undefined（void方法）
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

/**
 * 绑定视图模型实例到artboard
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppSetViewModelInstance
 */
napi_value ArtboardSetViewModelInstance(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and view model instance reference");
        return nullptr;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    // 获取视图模型实例指针
    int64_t viewModelInstanceRef;
    status = napi_get_value_int64(env, args[1], &viewModelInstanceRef);
    if (status != napi_ok) {
        LOGE("Failed to get view model instance reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    auto instance = reinterpret_cast<rive::ViewModelInstanceRuntime *>(viewModelInstanceRef);
    if (!instance) {
        LOGE("Invalid view model instance reference");
        return nullptr;
    }
    // 绑定视图模型实例到artboard
    artboard->bindViewModelInstance(instance->instance());
    LOGI("ViewModel instance bound to artboard successfully");
    // 返回undefined（void方法）
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

/**
 * 绘制artboard
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppDraw
 */
napi_value ArtboardDraw(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2];
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        LOGE("Invalid arguments - expected artboard reference and renderer reference");
        return nullptr;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }

    int64_t rendererRef;
    status = napi_get_value_int64(env, args[1], &rendererRef);
    if (status != napi_ok) {
        LOGE("Failed to get renderer reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }

    auto wrapper = reinterpret_cast<Renderer *>(rendererRef);
    if (!wrapper) {
        LOGE("Invalid renderer reference");
        return nullptr;
    }

    auto *renderer = wrapper->GetRendererOnWorkerThread();
    if (!renderer) {
        LOGE("Renderer not available in main thread");
        napi_value undefined;
        napi_get_undefined(env, &undefined);
        return undefined;
    }
    artboard->draw(renderer);
    LOGI("Artboard drawn successfully");
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

// Java_app_rive_runtime_kotlin_core_Artboard_cppDrawAligned
napi_value ArtboardDrawAligned(napi_env env, napi_callback_info info)
{
    size_t argc = 5;
    napi_value args[5];
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 5) {
        LOGE("Invalid arguments - expected artboard reference, renderer reference, fit, alignment and scale factor");
        return nullptr;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    int64_t rendererRef;
    status = napi_get_value_int64(env, args[1], &rendererRef);
    if (status != napi_ok) {
        LOGE("Failed to get renderer reference");
        return nullptr;
    }

    double scaleFactor;
    status = napi_get_value_double(env, args[4], &scaleFactor);
    if (status != napi_ok) {
        LOGE("Failed to get scale factor");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    auto wrapper = reinterpret_cast<Renderer *>(rendererRef);
    if (!wrapper) {
        LOGE("Invalid renderer reference");
        return nullptr;
    }

    auto *renderer = wrapper->GetRendererOnWorkerThread();
    if (!renderer) {
        LOGE("Renderer not available in main thread");
        napi_value undefined;
        napi_get_undefined(env, &undefined);
        return undefined;
    }

    auto fit = GetFit(env, args[2]);
    auto alignment = GetAlignment(env, args[3]);

    float width = static_cast<float>(wrapper->GetWidth());
    float height = static_cast<float>(wrapper->GetHeight());

    renderer->save();
    renderer->align(fit, alignment, rive::AABB(0, 0, width, height), artboard->bounds(),
                    static_cast<float>(scaleFactor));
    artboard->draw(renderer);
    renderer->restore();

    LOGI("Artboard drawn aligned with scale: %{public}f", scaleFactor);
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

/**
 * 删除artboard
 * 对应原 JNI Java_app_rive_runtime_kotlin_core_Artboard_cppDelete
 */
napi_value ArtboardDelete(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected artboard reference");
        return nullptr;
    }
    int64_t artboardRef;
    status = napi_get_value_int64(env, args[0], &artboardRef);
    if (status != napi_ok) {
        LOGE("Failed to get artboard reference");
        return nullptr;
    }
    auto artboard = reinterpret_cast<rive::ArtboardInstance *>(artboardRef);
    if (!artboard) {
        LOGE("Invalid artboard reference");
        return nullptr;
    }
    // 删除artboard实例
    delete artboard;
    // 返回undefined（void方法）
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

} // namespace ohos_rive